<template>
  <div class="meetings-container">
    <el-card shadow="never" class="header-card">
      <div class="page-header">
        <div class="header-content">
          <el-icon class="header-icon" :size="28"><UserFilled /></el-icon>
          <div class="header-text">
            <h2>会议管理</h2>
            <p>会议预定和管理</p>
          </div>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="showDialog = true">
            <el-icon><Plus /></el-icon>
            预定会议
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 会议统计 -->
    <div class="stats-row">
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#409EFF"><Calendar /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.total }}</div>
            <div class="stat-label">全部会议</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#E6A23C"><Clock /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.upcoming }}</div>
            <div class="stat-label">待召开</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#67C23A"><SuccessFilled /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.completed }}</div>
            <div class="stat-label">已完成</div>
          </div>
        </div>
      </el-card>
      <el-card shadow="hover" class="stat-card">
        <div class="stat-content">
          <el-icon class="stat-icon" :size="40" color="#909399"><OfficeBuilding /></el-icon>
          <div class="stat-info">
            <div class="stat-value">{{ stats.rooms }}</div>
            <div class="stat-label">会议室</div>
          </div>
        </div>
      </el-card>
    </div>

    <el-row :gutter="20">
      <el-col :span="16">
        <!-- 会议列表 -->
        <el-card shadow="never">
          <el-tabs v-model="activeTab">
            <el-tab-pane label="我的会议" name="my" />
            <el-tab-pane label="我组织的" name="organized" />
            <el-tab-pane label="我参与的" name="attended" />
          </el-tabs>

          <el-table :data="filteredMeetings" stripe>
            <el-table-column prop="title" label="会议主题" min-width="200" />
            <el-table-column prop="room" label="会议室" width="120" />
            <el-table-column label="时间" width="280">
              <template #default="{ row }">
                {{ row.date }} {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column prop="organizer" label="组织者" width="100" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getStatusType(row.status)">
                  {{ getStatusName(row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="250" fixed="right">
              <template #default="{ row }">
                <el-button link type="primary" @click="handleView(row)">
                  <el-icon><View /></el-icon>
                  详情
                </el-button>
                <el-button link type="success" @click="handleEdit(row)">
                  <el-icon><Edit /></el-icon>
                  编辑
                </el-button>
                <el-button link type="danger" @click="handleCancel(row)" v-if="row.status === 'upcoming'">
                  <el-icon><Close /></el-icon>
                  取消
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="8">
        <!-- 会议室状态 -->
        <el-card shadow="never" class="side-panel">
          <div class="panel-title">
            <el-icon><OfficeBuilding /></el-icon>
            <span>会议室状态</span>
          </div>
          <el-divider />
          <div class="room-list">
            <div
              v-for="room in rooms"
              :key="room.id"
              class="room-item"
              :class="room.status"
            >
              <div class="room-header">
                <span class="room-name">{{ room.name }}</span>
                <el-tag :type="room.status === 'available' ? 'success' : 'danger'" size="small">
                  {{ room.status === 'available' ? '空闲' : '使用中' }}
                </el-tag>
              </div>
              <div class="room-info">
                <span class="room-capacity">容纳：{{ room.capacity }}人</span>
                <span class="room-location">{{ room.location }}</span>
              </div>
              <div class="room-equipment">
                <el-tag v-for="eq in room.equipment" :key="eq" size="small" effect="plain">
                  {{ eq }}
                </el-tag>
              </div>
              <el-button
                v-if="room.status === 'available'"
                type="primary"
                size="small"
                @click="handleBookRoom(room)"
                style="width: 100%; margin-top: 8px;"
              >
                立即预定
              </el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 新建/编辑会议对话框 -->
    <el-dialog
      v-model="showDialog"
      :title="dialogTitle"
      width="700px"
      destroy-on-close
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="会议主题" prop="title">
          <el-input v-model="form.title" placeholder="请输入会议主题" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="会议日期" prop="date">
              <el-date-picker
                v-model="form.date"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议室" prop="room">
              <el-select v-model="form.room" placeholder="请选择会议室" style="width: 100%">
                <el-option
                  v-for="room in rooms.filter(r => r.status === 'available')"
                  :key="room.id"
                  :label="room.name"
                  :value="room.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-time-picker
                v-model="form.startTime"
                placeholder="选择时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-time-picker
                v-model="form.endTime"
                placeholder="选择时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="参会人员" prop="attendees">
          <el-select v-model="form.attendees" multiple placeholder="请选择参会人员" style="width: 100%">
            <el-option label="张三" value="1" />
            <el-option label="李四" value="2" />
            <el-option label="王五" value="3" />
            <el-option label="赵六" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="会议议程" prop="agenda">
          <el-input v-model="form.agenda" type="textarea" :rows="4" placeholder="请输入会议议程" />
        </el-form-item>
        <el-form-item label="会议备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入会议备注" />
        </el-form-item>
        <el-form-item label="提醒设置" prop="reminder">
          <el-select v-model="form.reminder" placeholder="请选择提醒时间" style="width: 100%">
            <el-option label="不提醒" value="none" />
            <el-option label="会议开始时" value="0" />
            <el-option label="提前15分钟" value="15" />
            <el-option label="提前30分钟" value="30" />
            <el-option label="提前1小时" value="60" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="handleSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  UserFilled, Plus, Calendar, Clock, SuccessFilled, OfficeBuilding,
  View, Edit, Close
} from '@element-plus/icons-vue'

// 状态
const activeTab = ref('my')

// 统计数据
const stats = reactive({
  total: 68,
  upcoming: 15,
  completed: 48,
  rooms: 8
})

// 会议数据
const meetings = ref([
  {
    id: 1,
    title: '月度工作总结会',
    room: '会议室A',
    date: '2024-01-18',
    startTime: '14:00',
    endTime: '16:00',
    organizer: '张三',
    status: 'upcoming'
  },
  {
    id: 2,
    title: '项目启动会',
    room: '会议室B',
    date: '2024-01-19',
    startTime: '09:00',
    endTime: '11:00',
    organizer: '李四',
    status: 'upcoming'
  },
  {
    id: 3,
    title: '需求评审会',
    room: '会议室C',
    date: '2024-01-15',
    startTime: '10:00',
    endTime: '12:00',
    organizer: '王五',
    status: 'completed'
  }
])

// 会议室数据
const rooms = ref([
  {
    id: 1,
    name: '会议室A',
    capacity: 20,
    location: '1楼',
    status: 'available',
    equipment: ['投影仪', '白板', '视频会议']
  },
  {
    id: 2,
    name: '会议室B',
    capacity: 10,
    location: '2楼',
    status: 'occupied',
    equipment: ['投影仪', '白板']
  },
  {
    id: 3,
    name: '会议室C',
    capacity: 30,
    location: '3楼',
    status: 'available',
    equipment: ['投影仪', '白板', '视频会议', '音响']
  },
  {
    id: 4,
    name: '会议室D',
    capacity: 8,
    location: '2楼',
    status: 'available',
    equipment: ['白板']
  }
])

// 对话框
const showDialog = ref(false)
const formRef = ref(null)
const currentMeeting = ref(null)
const dialogTitle = computed(() => currentMeeting.value ? '编辑会议' : '预定会议')

// 表单
const form = reactive({
  title: '',
  date: null,
  room: '',
  startTime: null,
  endTime: null,
  attendees: [],
  agenda: '',
  remark: '',
  reminder: '15'
})

// 表单验证规则
const rules = {
  title: [{ required: true, message: '请输入会议主题', trigger: 'blur' }],
  date: [{ required: true, message: '请选择会议日期', trigger: 'change' }],
  room: [{ required: true, message: '请选择会议室', trigger: 'change' }],
  startTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
  endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
}

// 计算属性
const filteredMeetings = computed(() => {
  return meetings.value
})

// 方法
const handleView = (row) => {
  ElMessageBox.alert(
    `会议室：${row.room}\n时间：${row.date} ${row.startTime}-${row.endTime}\n组织者：${row.organizer}`,
    row.title,
    { confirmButtonText: '确定', type: 'info' }
  )
}

const handleEdit = (row) => {
  currentMeeting.value = row
  Object.assign(form, {
    title: row.title,
    date: row.date,
    room: rooms.value.find(r => r.name === row.room)?.id,
    startTime: row.startTime,
    endTime: row.endTime,
    attendees: [],
    agenda: '会议议程...',
    remark: '',
    reminder: '15'
  })
  showDialog.value = true
}

const handleCancel = (row) => {
  ElMessageBox.confirm('确定要取消该会议吗？', '提示', {
    type: 'warning'
  }).then(() => {
    row.status = 'cancelled'
    ElMessage.success('会议已取消')
  }).catch(() => {})
}

const handleBookRoom = (room) => {
  form.room = room.id
  showDialog.value = true
}

const handleSave = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('保存成功')
      showDialog.value = false
    }
  })
}

const getStatusName = (status) => {
  const map = {
    upcoming: '待召开',
    completed: '已完成',
    cancelled: '已取消'
  }
  return map[status] || status
}

const getStatusType = (status) => {
  const map = {
    upcoming: 'warning',
    completed: 'success',
    cancelled: 'info'
  }
  return map[status] || ''
}
</script>

<style scoped>
.meetings-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  color: #409EFF;
}

.header-text h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.header-text p {
  margin: 4px 0 0 0;
  font-size: 14px;
  color: #909399;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.stat-card {
  cursor: pointer;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-4px);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.side-panel {
  height: fit-content;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  color: #303133;
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.room-item {
  padding: 16px;
  border: 1px solid #EBEEF5;
  border-radius: 8px;
  transition: all 0.3s;
}

.room-item:hover {
  border-color: #409EFF;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.room-item.available {
  background-color: #f0f9ff;
}

.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.room-name {
  font-size: 16px;
  font-weight: 500;
}

.room-info {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: #606266;
  margin-bottom: 8px;
}

.room-equipment {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
</style>

